<?
function checkExtension($filename, $allowList)
{
	$sExtension = $filename;
	$allowArray	= explode(',', $allowList);
	$allowPass	= 0;
	$count = count($allowArray);
	for($i=0;$i<$count;$i++)
	{
		if ($sExtension == $allowArray[$i])
		{
			$allowPass = 1;
		}
	}
	
	return $allowPass;
}

function check_upload_extension($filename, $allow_list)
{
	$sExtension = getExtension($filename);
	$allow_arr = explode(',', $allow_list);
	$pass = 0;
	$count = count($allow_arr);
	for($i=0;$i<$count;$i++)
	{
		if ($sExtension == $allow_arr[$i])
		{
			$pass = 1;
		}
	}
	
	return $pass;
}

function delete_file($path, $filename)
{
	if ($filename == '')
	{
		return;
	}
	$array_file	= array('small_', 'normal_', 'larger_', '');
	$count = count($array_file);
	for($i=0;$i<$count;$i++)
	{
		if (file_exists($path . $array_file[$i] . $filename))
		{
			@unlink($path . $array_file[$i] . $filename);
		}
	}
}

function delete_picture_more($type, $record_id, $multi = 0, $path = '../../')
{
	global $lang_id;
	$gs_table = $type . '_pictures';
	$prefix = '';
	switch ($type)
	{
		case 'tour': $prefix = 'tp'; break;
	}
	$id_field				= ($multi != 1) ? $prefix . '_id' : $prefix . '_' . $type . '_id';
	$gs_fieldupload			= $prefix . '_picture';
	$gs_filepath_fullsize	= $path . $type . '_pictures_more/fullsize/';
	$gs_filepath_small		= $path . $type . '_pictures_more/small/';
	$gs_filepath_normal		= $path . $type . '_pictures_more/normal/';
	
	$db_check = new db_query('SELECT ' . $gs_fieldupload . ' FROM ' . $gs_table . ' WHERE ' . $id_field . ' = ' . $record_id . ' AND lang_id = ' . $lang_id);
	while ($check = mysql_fetch_array($db_check->result))
	{
		if ($check[$gs_fieldupload] != '')
		{
			delete_file($gs_filepath_fullsize, $check[$gs_fieldupload]);
			delete_file($gs_filepath_small, $check[$gs_fieldupload]);
			delete_file($gs_filepath_normal, $check[$gs_fieldupload]);
		}
		$db_execute = new db_execute('DELETE FROM ' . $gs_table . ' WHERE ' . $id_field . ' = ' . $record_id);
		unset($db_execute);
	}
	unset($db_check);
}

function generate_name($filename)
{
	$name = '';
	for($i=0;$i<3;$i++)
	{
		$name .= chr(rand(97,122));
	}
	$today = getdate();
	$name .= $today[0];
	$ext = substr($filename, (strrpos($filename, '.') + 1));
	
	return $name . '.' . $ext;
}

function getExtension($filename)
{
	$sExtension = substr($filename, (strrpos($filename, '.') + 1));
	$sExtension = strtolower($sExtension);
	return $sExtension;
}